<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\BaseinitController;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class ProductController extends BaseinitController
{
    public function pageCourses(Request $request)
    {
        $openid=$request->input('openid');
        $pageNum=$request->input('page');
        $limit=$request->input('limit');
        $joined=$request->input('joined');
        $page=$pageNum-1;
        if ($page != 0) {
            $page = $limit * $page;
        }
        if($joined==0){
            $map = [
                ['course.state',1]
            ];
            $course = DB::table('course')
                ->leftJoin('teacher', 'teacher.id', '=', 'course.t_id')
                ->leftJoin('orders', 'course.id', '=', 'orders.course_id')
                ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                ->where($map)
                ->orderBy('course.recommend','asc')
                ->orderBy('course.uptime','desc')
                ->orderBy('course.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('course.*','teacher.teacher', 'orders.openid', 'orders.state as isJoined')
                ->get();
            foreach ($course as $key => $value) {
                if($course[$key]->openid==$openid){
                    $course[$key]->isJoined=2;
                    $course[$key]->openid='';
                }else{
                    $course[$key]->isJoined=1;
                    $course[$key]->openid='';
                }
                // $course[$key]->detail=mb_substr($course[$key]->detail,0,32);
            }
        }
        if($joined==1){
            $map = [
                ['orders.state',2],
                ['course.state',1],
                ['orders.openid',$openid]
            ];
            $course = DB::table('course')
                ->leftJoin('teacher', 'teacher.id', '=', 'course.t_id')
                ->leftJoin('orders', 'course.id', '!=', 'orders.course_id')
                ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                ->where($map)
                ->orderBy('course.recommend','asc')
                ->orderBy('course.uptime','desc')
                ->orderBy('course.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('course.*','teacher.teacher', 'orders.state as isJoined')
                ->get();
            if(count($course)>0){
                foreach ($course as $key => $value) {
                    $course[$key]->isJoined=1;
                    // $course[$key]->detail=mb_substr($course[$key]->detail,0,32);
                }
            }else{
                $map = [
                    ['course.state',1]
                ];
                $course = DB::table('course')
                    ->leftJoin('teacher', 'teacher.id', '=', 'course.t_id')
                    ->leftJoin('orders', 'course.id', '=', 'orders.course_id')
                    ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                    ->where($map)
                    ->orderBy('course.recommend','asc')
                    ->orderBy('course.uptime','desc')
                    ->orderBy('course.id','desc')
                    ->offset($page)
                    ->take($limit)
                    ->select('course.*','teacher.teacher', 'orders.openid', 'orders.state as isJoined')
                    ->get();
                foreach ($course as $key => $value) {
                    if($course[$key]->openid==$openid){
                        $course[$key]->isJoined=2;
                        $course[$key]->openid='';
                    }else{
                        $course[$key]->isJoined=1;
                        $course[$key]->openid='';
                    }
                    // $course[$key]->detail=mb_substr($course[$key]->detail,0,32);
                }
            }
        }
        if($joined==2){
            $map = [
                ['orders.state',2],
                ['course.state',1],
                ['orders.openid',$openid]
            ];
            $course = DB::table('course')
                ->leftJoin('teacher', 'teacher.id', '=', 'course.t_id')
                ->leftJoin('orders', 'course.id', '=', 'orders.course_id')
                ->leftJoin('users', 'orders.user_id', '=', 'users.id')
                ->where($map)
                ->orderBy('course.recommend','asc')
                ->orderBy('course.uptime','desc')
                ->orderBy('course.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('course.*','teacher.teacher', 'orders.state as isJoined')
                ->get();
        }
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'data'  => $course
        ];
        return response()->json($data);
    }
    public function courseDetail(Request $request)
    {
        $http_type = $this->is_https();
        if ($http_type) {
            $http='https://';
        }else{
            $http='http://';
        }
        $id=$request->input('id');
        $openid=$request->input('openid');
        $course =DB::table('course')
                ->leftJoin('teacher', 'teacher.id', '=', 'course.t_id')
                ->where('course.id',$id)
                ->select('course.*', 'teacher.avatar', 'teacher.teacher')
                ->first();
        if($course){
            $course->detail=str_replace('/ueditor/php/upload/image/',$http.$_SERVER["HTTP_HOST"].'/ueditor/php/upload/image/',$course->detail);
            $course->detail=str_replace('/ueditor/php/upload/video/',$http.$_SERVER["HTTP_HOST"].'/ueditor/php/upload/video/',$course->detail);
        }
        $buyed=DB::table('orders')->where('openid',$openid)->where('course_id',$id)->where('state',2)->first();
        if($buyed){
            $buyed=true;
        }else{
            if($course->price>0){
                $buyed=false;
                $course->detail=mb_substr($course->detail,0,32);
            }else{
                $buyed=true;
            }
        }
        $data = [
            'code' => 200,
            'msg'   => '请求成功',
            'course'  => $course,
            'buyed'  => $buyed
        ];
        return response()->json($data);
    }
	public function products(Request $request)
	{
        $pageNum=$request->input('page');
        $searchOntype=$request->input('searchOntype');
        $priceAd=$request->input('priceAd');
        $limit=10;
        $page=$pageNum-1;
        if ($page != 0) {
            $page = $limit * $page;
        }
        $map = [
            ['state',1]
        ];
        if($searchOntype==1){
            $course = DB::table('course')
                ->where($map)
                ->orderBy('recommend','asc')
                ->orderBy('uptime','desc')
                ->orderBy('id','desc')
                ->offset($page)
                ->take($limit)
                ->get();
        }
        if($searchOntype==2){
            if($priceAd==1){
                $course = DB::table('course')
                    ->where($map)
                    ->orderBy('price','asc')
                    ->offset($page)
                    ->take($limit)
                    ->get();
            }else{
                $course = DB::table('course')
                    ->where($map)
                    ->orderBy('price','desc')
                    ->offset($page)
                    ->take($limit)
                    ->get();
            }
        }
        if($searchOntype==3){
            $course = DB::table('course')
                ->where($map)
                ->orderBy('id','desc')
                ->offset($page)
                ->take($limit)
                ->get();
        }
        if($searchOntype==4){
            $course = DB::table('course')
                ->where($map)
                ->orderBy('sold','asc')
                ->offset($page)
                ->take($limit)
                ->get();
        }
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'data'  => $course
        ];
        return response()->json($data);
	}
	public function productSearch(Request $request)
	{
        $pageNum=$request->input('page');
        $key=$request->input('key');
        $searchOntype=$request->input('searchOntype');
        $priceAd=$request->input('priceAd');
        $limit=10;
        $page=$pageNum-1;
        if ($page != 0) {
            $page = $limit * $page;
        }
        $map = [
            ['state',1]
        ];
        if($searchOntype==1){
            $course = DB::table('course')
                ->whereRaw("concat(`course`) like '%".$key."%'")
                ->where($map)
                ->orderBy('recommend','asc')
                ->orderBy('uptime','desc')
                ->orderBy('id','desc')
                ->offset($page)
                ->take($limit)
                ->get();
        }
        if($searchOntype==2){
            if($priceAd==1){
                $course = DB::table('course')
                    ->whereRaw("concat(`course`) like '%".$key."%'")
                    ->where($map)
                    ->orderBy('price','asc')
                    ->offset($page)
                    ->take($limit)
                    ->get();
            }else{
                $course = DB::table('course')
                    ->whereRaw("concat(`course`) like '%".$key."%'")
                    ->where($map)
                    ->orderBy('price','desc')
                    ->offset($page)
                    ->take($limit)
                    ->get();
            }
        }
        if($searchOntype==3){
            $course = DB::table('course')
                ->whereRaw("concat(`course`) like '%".$key."%'")
                ->where($map)
                ->orderBy('id','desc')
                ->offset($page)
                ->take($limit)
                ->get();
        }
        if($searchOntype==4){
            $course = DB::table('course')
                ->whereRaw("concat(`course`) like '%".$key."%'")
                ->where($map)
                ->orderBy('sold','asc')
                ->offset($page)
                ->take($limit)
                ->get();
        }
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'data'  => $course
        ];
        return response()->json($data);
	}

    public function productDetail(Request $request)
    {
        $http_type = $this->is_https();
        if ($http_type) {
            $http='https://';
        }else{
            $http='http://';
        }
        $id=$request->input('id');
        $openid=$request->input('openid');
        $course =DB::table('course')
                ->leftJoin('teacher', 'teacher.id', '=', 'course.t_id')
                ->where('course.id',$id)
                ->select('course.*', 'teacher.avatar', 'teacher.teacher')
                ->first();
        if($course){
            $course->detail=str_replace('/ueditor/php/upload/image/',$http.$_SERVER["HTTP_HOST"].'/ueditor/php/upload/image/',$course->detail);
        }
        $collect=DB::table('collect')->where('openid',$openid)->where('c_id',$id)->where('state',1)->first();
        if($collect){
            $collected=1;
        }else{
            $collected=2;
        }
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'course'  => $course,
            'collected'  => $collected
        ];
        return response()->json($data);
    }
    public function evaluation(Request $request)
    {
        $id=$request->input('id');
        $pageNum=$request->input('page');
        $limit=10;
        $page=$pageNum-1;
        if ($page != 0) {
            $page = $limit * $page;
        }
        $evaluate =DB::table('evaluate')
                ->leftJoin('users', 'evaluate.openid', '=', 'users.openid')
                ->where('evaluate.c_id',$id)
                ->orderBy('evaluate.id','desc')
                ->offset($page)
                ->take($limit)
                ->select('evaluate.*', 'users.avatar', 'users.nickname')
                ->get();
        if ($evaluate) {
            foreach ($evaluate as $value) {
                $value->time=date('Y-m-d',$value->time);
            }
        }
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'data'  => $evaluate
        ];
        return response()->json($data);
    }
    private function is_https() {
        if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
            return true;
        } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
            return true;
        } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
            return true;
        }
        return false;
    }
}